package easy

import (
	. "GoLeetcode/common"
	"math"
)

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
/*
TODO:是否可以不用辅助内存处理(二叉搜索树性质)
 */

func minDiffInBST(root *TreeNode) int {
	nodes := make([]int, 0)
	var dfs func(node *TreeNode)
	dfs = func(node *TreeNode) {
		if node == nil {
			return
		}
		dfs(node.Left)
		nodes = append(nodes, node.Val)
		dfs(node.Right)
	}
	dfs(root)

	diff := math.MaxInt32
	for i := 1; i < len(nodes); i++ {
		tempDiff := nodes[i] - nodes[i-1]
		if diff > tempDiff {
			diff = tempDiff
		}
	}
	return diff
}
